#include "stdafx.h"

//grid output
int outputGrid(int *grid, int row, int col)
{
	char *filename = (char *)malloc(sizeof(char) * 256);
	sprintf(filename, "grid_r%d_c%d.txt", row, col);

	FILE *fp;
	if((fp = fopen(filename, "at")) == NULL)
	{
		printf("cannot open %s \n", filename);
		return 0;
	}
	else
	{
		printf("output grid row - %d  col - %d\n", row, col);
		for(int i = 0; i < row; i++)              
		{
			for(int j = 0; j < col; j++)                      
				fprintf(fp, "%d ", grid[i * col + j]);
			fprintf(fp, "\n");
		}
		fclose(fp);
	}
	return 0;
}

//temperature field output
int outputTemperatureField(double *grid, int row, int col, double time)
{
	char *filename = (char *)malloc(sizeof(char) * 256);
	sprintf(filename, "tempField_r%d_c%d_%.3f.txt", row, col, time);

	FILE *fp;
	if((fp = fopen(filename, "at")) == NULL)
	{
		printf("cannot open %s \n", filename);
		return 0;
	}
	else
	{
		printf("output grid row - %d  col - %d @ time - %f\n", row, col, time);
		for(int i = 0; i < row; i++)              
		{
			for(int j = 0; j < col; j++)                      
				fprintf(fp, "%f ", grid[i * col + j]);
			fprintf(fp, "\n");
		}
		fclose(fp);
	}
	return 0;
}